package com.glb.gilibuy.web.mall;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glb.gilibuy.entity.UserReceiveAddress;
import com.glb.gilibuy.util.BeanUtil;
import com.glb.gilibuy.util.MapGenerator;
import com.glb.gilibuy.web.BaseController;
import com.glb.gilibuy.web.vo.UserReceiveAddressVO;
import com.glb.gilibuy.web.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Description: 备注：这个文件的增 删 查 改，可作为通用的参考
 * @Author: Yingtian qian
 * @Date: 2021-08-09 16:38
 */
@Api(tags = "用户收货地址模块")
@Controller
@RequestMapping("/user")
public class AddressController extends BaseController {
    @ApiOperation(value = "添加收货地址", notes = "添加收货地址", produces = "application/json")
    @PostMapping("/addAddress")
    @ResponseBody
    public Map<String, Object> addAddress(@RequestParam("name") String name,
                                          @RequestParam("phone") String phone,
                                          @RequestParam("province") String province,
                                          @RequestParam("city") String city,
                                          @RequestParam("region") String region,
                                          @RequestParam("detail") String detail,
                                          @RequestParam(value = "isDefault", defaultValue = "false") boolean isDefault,
                                          HttpSession httpSession) {
        /**如果缺参数，或参数为“”，或isDefault不符合规则，直接报500.html*/

        UserVO userTemp = (UserVO) httpSession.getAttribute("user");

        //手机号格式
        String regex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        if (!phone.matches(regex)) {
            return MapGenerator.getRes(500, "手机号格式有误！");
        }

        UserReceiveAddress userReceiveAddress = new UserReceiveAddress();

        //添加地址信息：只对手机号有校参，其余参数不为空即可，isDefault由于框架限制必为规定格式
        userReceiveAddress.setUserId(userTemp.getUserId());
        userReceiveAddress.setReceiveName(name);
        userReceiveAddress.setReceivePhone(phone);
        userReceiveAddress.setReceiveProvince(province);
        userReceiveAddress.setReceiveCity(city);
        userReceiveAddress.setReceiveRegion(region);
        userReceiveAddress.setReceiveAddressDetail(detail);
        if (isDefault) {
            userReceiveAddress.setIsDefault(1);
        } else {
            userReceiveAddress.setIsDefault(0);
        }

        return userReceiveAddressService.addAddress(userReceiveAddress);
    }

    @ApiOperation(value = "获取收货地址列表", notes = "获取收货地址列表", produces = "application/json")
    @GetMapping("/addressList")
    @ResponseBody
    public Map<String, Object> getAddressList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                              HttpSession httpSession) {
        UserVO userTemp = (UserVO) httpSession.getAttribute("user");
        //每页6个
        PageHelper.startPage(page, 6);
        PageInfo pageInfo = userReceiveAddressService.findAddressesByUserId(userTemp.getUserId());
        if (pageInfo != null) {
            return MapGenerator.getResWithData(200, "获取收货地址列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "获取结果为空！");
        }
    }

    @ApiOperation(value = "修改收货地址", notes = "修改收货地址", produces = "application/json")
    @PostMapping("/updateAddress")
    @ResponseBody
    public Map<String, Object> updateAddress(@RequestBody UserReceiveAddressVO userReceiveAddressVO,
                                             HttpSession httpSession) {
        /**这是一个通用的修改接口；如果JSON格式有误，直接报500.html；除了addressId，其他参数都是非必须的*/

        if (StringUtils.isEmpty(userReceiveAddressVO.getAddressId())) {
            return MapGenerator.getRes(500, "要修改的地址id不能为空！");
        }

        UserVO userTemp = (UserVO) httpSession.getAttribute("user");

        userReceiveAddressVO.setUserId(userTemp.getUserId());
        UserReceiveAddress userReceiveAddress = new UserReceiveAddress();
        BeanUtil.copyOne(userReceiveAddressVO, userReceiveAddress);

        return userReceiveAddressService.updateAddress(userReceiveAddress);
    }

    @ApiOperation(value = "删除收货地址", notes = "删除收货地址", produces = "application/json")
    @PostMapping("/deleteAddress")
    @ResponseBody
    public Map<String, Object> deleteAddress(@RequestParam("addressId") String addressId,
                                             HttpSession httpSession) {
        /**这是一个特化的修改接口*/
        UserVO userTemp = (UserVO) httpSession.getAttribute("user");

        UserReceiveAddress userReceiveAddress = new UserReceiveAddress();
        userReceiveAddress.setUserId(userTemp.getUserId());
        userReceiveAddress.setAddressId(addressId);

        return userReceiveAddressService.deleteAddress(userReceiveAddress);
    }
}
